package com.zqxq.lottery.shedule;

import com.alibaba.fastjson.JSONObject;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.service.order.IFollowOrderService;
import com.zqxq.lottery.service.sys.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 苦智
 * @date 2018/11/6 14:17
 * @description 推单机器人
 * @ClassName FollowOrderRobot
 */
@Component
@Slf4j
public class FollowOrderRobot {
    @Autowired
    private IFollowOrderService followOrderService;
    @Autowired
    private ISysConfigService configService;

    @Scheduled(cron="0 23 0,6,8,9,10,14,15,16,18,20,22 * * ? ")
    public void executeCreateOrder(){
        try {
            String val = configService.getValueByKey("FOLLOW_ORDER_ROBOT");
            if (StrUtils.isNotEmpty(val)) {
                val = JSONObject.parseObject(val, Map.class).get("ROBOT").toString();
                if ("1".equals(val)) {
                    String timeStr = DateUtils.getCurrentDateTimeStr();
                    log.info("机器人推单开始,推单时间={}", timeStr);
                    followOrderService.followOrderRobot();
                }
            }
        }catch (Exception e){
            log.error("机器人推单故障",e);
        }
    }

    @Scheduled(cron="0 0/30 0,1,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 * * ? ")
    public void executeRobotPropularity(){
        try {
            String val = configService.getValueByKey("FOLLOW_ORDER_ROBOT");
            if (StrUtils.isNotEmpty(val)) {
                val = JSONObject.parseObject(val, Map.class).get("CREATE_POPU").toString();
                if ("1".equals(val)) {
                    String timeStr = DateUtils.getCurrentDateTimeStr();
                    log.info("创建机器人跟单人气,创建时间={}", timeStr);
                    followOrderService.createRobotPropularity();
                }
            }
        }catch (Exception e){
            log.error("机器人故障",e);
        }
    }

}
